package com.uit.trip.utils;

import java.net.URL;

import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import twitter4j.conf.Configuration;
import twitter4j.conf.ConfigurationBuilder;
import twitter4j.http.AccessToken;
import twitter4j.http.OAuthAuthorization;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

import com.uit.trip.R;
import com.uit.trip.bridge.Server;
import com.uit.trip.twitter.TwitterApp;
import com.uit.trip.twitter.TwitterApp.TwDialogListener;
import com.uit.trip.twitter.TwitterSession;

public class ShareTwitter {
	private TwitterApp mTwitter;
	private static final String twitter_consumer_key = "G6jQg6aqfApQFspjMGvayA";
	private static final String twitter_secret_key = "LJnxlaeYRGS3TtwiDdeeqKcH3yRSrmd6OEN6LC2XvtU";
	private static final String twitpic_api_key = "777859dc44efb6df950d052fdfd53f74";
	private Context context;
	private ProgressDialog mProgressDialog;
	private String resultPath;

	public ShareTwitter(Context _context) {
		context = _context;
	}

	public void sharePhotoViaTwitter(String link) {
		Log.i("duongnx", "share twitter");
		mTwitter = new TwitterApp(context, twitter_consumer_key,
				twitter_secret_key);
		resultPath = Server.LINKSHARE + link;
		mTwitter.setListener(mTwLoginDialogListener);
		if (mTwitter.hasAccessToken()) {
			new ImageSender().execute();
		} else {
			mTwitter.authorize();
		}
	}

	private final TwDialogListener mTwLoginDialogListener = new TwDialogListener() {
		@Override
		public void onComplete(String value) {
			Log.i("duongnx", "twitter:" + value);
			new ImageSender().execute();
		}

		@Override
		public void onError(String value) {
			Toast.makeText(context, "Twitter connection failed",
					Toast.LENGTH_LONG).show();
		}
	};

	private class ImageSender extends AsyncTask<URL, Integer, Long> {

		@Override
		protected void onPreExecute() {
			String username = mTwitter.getUsername();
			Toast.makeText(context, "Connected to Twitter as " + username,
					Toast.LENGTH_LONG).show();
			mProgressDialog = ProgressDialog.show(context, "",
					"Share image to Twitter.", true);
			mProgressDialog.setCancelable(false);
			mProgressDialog.show();
		}

		@Override
		protected Long doInBackground(URL... urls) {
			long result = 0;
			Log.i("duongnx", "doInBackground");
			TwitterSession twitterSession = new TwitterSession(context);
			AccessToken accessToken = twitterSession.getAccessToken();

			Configuration conf = new ConfigurationBuilder()
					.setOAuthConsumerKey(twitter_consumer_key)
					.setOAuthConsumerSecret(twitter_secret_key)
					.setOAuthAccessToken(accessToken.getToken())
					.setOAuthAccessTokenSecret(accessToken.getTokenSecret())
					.build();

			OAuthAuthorization auth = new OAuthAuthorization(conf,
					conf.getOAuthConsumerKey(), conf.getOAuthConsumerSecret(),
					new AccessToken(conf.getOAuthAccessToken(),
							conf.getOAuthAccessTokenSecret()));

			try {
				Twitter twitter = new TwitterFactory(conf).getInstance();
				twitter.updateStatus(resultPath);

				// ImageUpload upload = ImageUpload.getTwitpicUploader(
				// twitpic_api_key, auth);
				// url = upload.upload(new File(resultPath));
				result = 1;
			} catch (Exception e) {
				e.printStackTrace();
			}

			return result;
		}

		@Override
		protected void onProgressUpdate(Integer... progress) {
		}

		@Override
		protected void onPostExecute(Long result) {
			mProgressDialog.cancel();

			String text = (result == 1) ? "Share photo successfully."
					: "Failed to send image";

			AlertDialog.Builder builder = new AlertDialog.Builder(context);
			builder.setTitle("Twitter");
			builder.setIcon(R.drawable.twitter_icon);
			builder.setMessage(text)
					.setCancelable(true)
					.setPositiveButton("OK",
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									// TODO Auto-generated method stub
									dialog.dismiss();
								}
							});
			builder.create().show();
		}
	}
}
